package pl.edu.agh.mbc.server;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import pl.agh.edu.exceptions.DaoException;
import pl.agh.mbc.server.MBCPacket;
import pl.agh.mbc.server.MBCPacketResponse;
import pl.agh.mbc.server.MethodNums;
import pl.edu.agh.mbc.database.dao.commons.DaoControll;
import pl.edu.agh.mbc.persistance.Category;
import pl.edu.agh.mbc.persistance.User;

public class ServerHandler {
	private static final Logger LOG = Logger.getLogger(ServerHandler.class);
	private DaoControll controll;

	public ServerHandler(DaoControll controll) {
		this.controll = controll;
	}

	public MBCPacketResponse recognizeAndReturn(MBCPacket request) {
		try {
			LOG.debug("nClient request == " + request.getMethodCode()
					+ new Date());

			switch (request.getMethodCode()) {
			case MethodNums.DO_LOGIN: {
				User user = controll.getUsrDao().login(
						(String) request.getParameters()[0],
						(String) request.getParameters()[1]);

				return new MBCPacketResponse(MethodNums.DO_LOGIN, user);
			}
			case MethodNums.DO_GET_CATEGORYS: {
				List<Category> categorys = controll.getContentDao().getCategorys();
				return new MBCPacketResponse(MethodNums.DO_LOGIN, categorys);
			}
			case MethodNums.DO_SEARCH: {

				break;
			}
			case MethodNums.DO_LOGOUT: {

				break;
			}
			default: {
				LOG.error("unrecognized method code == "
						+ request.getMethodCode());
			}
			}

		} catch (DaoException e) {
			return new MBCPacketResponse(MethodNums.SYSTEM_ERROR, null);
		}

		return new MBCPacketResponse(MethodNums.SYSTEM_ERROR, null);
	}
}
